package pt.inescid.components.policyengine.heimdall.pdp.base;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.base.EventRelevance;
import pt.inescid.components.policyengine.heimdall.commons.Utilities;
import pt.inescid.components.policyengine.heimdall.pap.base.CesDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;

public class AggValue {

	public static final int SUM_VALUE_TYPE = 0;
	public static final int MAX_VALUE_TYPE = 1;
	public static final int MIN_VALUE_TYPE = 2;
	public static final int COUNT_VALUE_TYPE = 3;
	public static final int EXISTS_VALUE_TYPE = 4;
	public static final int NOT_EXISTS_VALUE_TYPE = 5;

	public static final int NUMBER_OF_VALUE_TYPES = 6;
	
	private EventRelevance cesInfo = null;

	private long[] value;
	private boolean[] valueTag;

	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      Constructor       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public AggValue(EventRelevance cesInfo) {
		super();
		// TODO Auto-generated constructor stub
		this.cesInfo = cesInfo;
	}

	public long[] getValue() {
		return value;
	}

	public boolean[] getValueTag() {
		return valueTag;
	}

	public void setValue(long[] value) {
		this.value = value;
	}

	public void setValueTag(boolean[] valueTag) {
		this.valueTag = valueTag;
	}

	public EventRelevance getCesInfo() {
		return cesInfo;
	}

	public void setCesInfo(EventRelevance cesInfo) {
		this.cesInfo = cesInfo;
	}

	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      isRelevant       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public boolean isRelevant(EventObj ce, ModelDefinition currentModelDef) {
		CesDefinition currentCes = currentModelDef.getCes(cesInfo.getCesName());
		
		return (currentCes.isRelevant(ce));
	}
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      updateValue       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public void updateValue(EventObj ce, 
			Vector allRelevantEventFields,
			ModelDefinition modelDef) {
		
		Vector allSingleRelevant = Utilities.removeDuplicates(
				allRelevantEventFields);
		
		EventRelevance currentRelevance = null;
		for (int i = 0; i < allSingleRelevant.size(); i++) {
			currentRelevance = (EventRelevance) allSingleRelevant.elementAt(i);
			
			if (currentRelevance.equals(cesInfo)) {
				long fieldValue = 
					((Long) (ce.getField(
							modelDef.getFieldId(
									currentRelevance.getFieldName())).
							getValue())).longValue(); 
				for (int j = 0; j < valueTag.length; j++) {
					if (valueTag[j]) {
						switch (j) {
						case SUM_VALUE_TYPE:
							value[j] += fieldValue;
							break;
						default:
							break;
						}
					}
				}
				
			}
		}
	}


}; // AggValue
